﻿// -----------------------------------------------------------------------
// <copyright file="FactCourseSubjectTrainerRepository.cs" company="AGR">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace AGR.Data.Repository.Contract
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using AGR.Model;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class FactCourseSubjectTrainerRepository : AbstractRepository<FactCourseSubjectTrainer>
    {
        public FactCourseSubjectTrainerRepository(ContextHelper contextHelper)
        {
            base._contextHelper = contextHelper;
            base._context = _contextHelper.GetContext();
        }

        public override IQueryable<FactCourseSubjectTrainer> GetAll()
        {
            var result = (from x in base._context.FactCourseSubjectTrainers
                          select new FactCourseSubjectTrainer
                          {
                              Id = x.Id,
                              CourseCode = x.CourseCode,
                              SubjectCode = x.SubjectCode,
                              EmployeeCode = x.EmployeeCode,
                              DateCreated = x.DateCreated,
                              UserCreated = x.UserCreated,
                              DateModified = x.DateModified,
                              UserModified = x.UserModified
                          });

            return result;
        }

        public override void Add(FactCourseSubjectTrainer entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.FactCourseSubjectTrainer entityToAdd = new Context.FactCourseSubjectTrainer
            {
                CourseCode = entity.CourseCode,
                SubjectCode = entity.SubjectCode,
                EmployeeCode = entity.EmployeeCode,
                DateCreated = DateTime.Now,
                UserCreated = entity.UserCreated,
                DateModified = null,
                UserModified = null
            };

            base._context.FactCourseSubjectTrainers.InsertOnSubmit(entityToAdd);
        }

        public override void Update(FactCourseSubjectTrainer entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.FactCourseSubjectTrainer entityToUpdate = base._context.FactCourseSubjectTrainers.Single(x => x.SubjectCode.Equals(entity.SubjectCode));

            entityToUpdate.CourseCode = entity.CourseCode;
            entityToUpdate.SubjectCode = entity.SubjectCode;
            entityToUpdate.EmployeeCode = entity.EmployeeCode;
            entityToUpdate.DateModified = DateTime.Now;
            entityToUpdate.UserModified = entity.UserModified;
        }

        public override void Delete(FactCourseSubjectTrainer entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.FactCourseSubjectTrainer entityToDelete = base._context.FactCourseSubjectTrainers.Single(x => x.SubjectCode == entity.SubjectCode &&
                                                                                                                  x.CourseCode == entity.CourseCode);

            base._context.FactCourseSubjectTrainers.DeleteOnSubmit(entityToDelete);
        }
    }
}
